package retrofitdemo.com.night.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import androidx.databinding.DataBindingUtil;

import com.amap.api.navi.INavi;
import com.lzy.okgo.callback.StringCallback;

import okhttp3.Call;
import okhttp3.Response;
import retrofitdemo.com.night.MainActivity;
import retrofitdemo.com.night.R;
import retrofitdemo.com.night.bean.LoginSuccBean;
import retrofitdemo.com.night.databinding.ActivityLoginBinding;
import retrofitdemo.com.night.utils.GsonUtil;
import retrofitdemo.com.night.utils.HTTPAPI;
import retrofitdemo.com.night.utils.LogUtils;
import retrofitdemo.com.night.utils.SPUtil;

@SuppressLint("Registered")
public class LoginActivity extends BaseActivity{

    ActivityLoginBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this,R.layout.activity_login);
        binding.setActivity(this);
        initView();
    }

    private void initView() {
        String isLogin = (String) SPUtil.get(LoginActivity.this,"module_login","");
        if (!TextUtils.isEmpty(isLogin)) {
            startActivity(new Intent(LoginActivity.this, MainActivity.class));
        }

        binding.btnModuleLogin.setOnClickListener(this);
        binding.tvModuleRegister.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        int sS_id = view.getId();
        if (sS_id == R.id.btnModuleLogin) {
            LogUtils.e("登录");
            login();
        }else if (sS_id == R.id.tvModuleRegister){
            startActivity(new Intent(LoginActivity.this,RegisterActivity.class));
        }
    }

    public void login(){
        String phone = binding.etModulePhone.getText().toString().trim();
        if (TextUtils.isEmpty(phone)) {
            showToast("请输入账号");
            return;
        }

        String pwd = binding.etMOdulePwd.getText().toString().trim();
        if (TextUtils.isEmpty(pwd)) {
            showToast("请输入密码");
            return;
        }

        HTTPAPI.getInstance().login(phone, pwd, new StringCallback() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                LogUtils.e("登录成功-->"+ s);
                LoginSuccBean bean = GsonUtil.GsonToBean(s,LoginSuccBean.class);

                if (bean.getCode().equals("200")) {
                    SPUtil.put(LoginActivity.this,"module_token",bean.getResult().getToken());
                    SPUtil.put(LoginActivity.this,"module_name",bean.getResult().getName());
                    SPUtil.put(LoginActivity.this,"module_phone",bean.getResult().getMobile());
                    startActivity(new Intent(LoginActivity.this, MainActivity.class));
                }else{
                    Toast.makeText(LoginActivity.this,bean.getMsg(),Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}
